<?php
class Application_Model_DonDatHang extends Zend_Db_Table
{
	protected $_name='dondathang';
	public function getLastId()
	{
		$qr='select * from dondathang where id>=all(select id from dondathang)';
		//echo "here";die;
		$db = $this->_db;
		$db->setFetchMode ( Zend_Db::FETCH_OBJ );
		$row =$db->fetchAll($qr);
		if (!$row) {
			throw new Exception("Khong tim thay don dat hang ");
		}
		return $row;
	}
	
	public function getDonDatHang($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Khong tim thay don dat hang $id");
		}
		return $row->toArray();
	}//phuong thuc tra ve mot dong theo $id

	public function addDonDatHang($khachhang_id,$ngaylap,$hinhthucthanhtoan,$formData)
	{
		
		$ngay_giohen = 	$formData['ngayhen'].' '.
						$formData['giohen'].'-'.
						$formData['phuthen'].'-'.
						'0';
		$data = array(
				'khachhang_id' => $khachhang_id,
				'hinhthucthanhtoan' => $hinhthucthanhtoan,
				'ngaydathang'=>$ngaylap,
				'ngay_giohengiaohang' =>$ngay_giohen,
				'tennguoinhan'=>$formData['tenngnhan'],
				'diachinhan'=>$formData['diachinhan'],
				'thongdiep'=>$formData['thongdiep']
		);
		$this->insert($data);
	}//insert
	
	public function updateDonDatHang($id)
	{
		$data=(array('trangthai'=>1));
		$this->update($data, 'id ='.(int)$id);
	}

	public function deleteDonDatHang($id)
	{
		$this->delete('id =' . (int)$id);
	}//drop
}